package crmdna.client.venue;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.venue.Venue.VenueProp;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.crmdna.Client;

@Api(name = "venue")
public class VenueApi {
	@ApiMethod(path="create", httpMethod = HttpMethod.POST)
	public APIResponse create(@Named("client") String client, 
			@Named("short_name") String displayName, 
			@Named("address") String address, @Named("group_id") long groupId, User user) {
		
		try {			
			VenueProp prop = Venue.create(client, displayName, address, groupId, 
					Utils.getLoginEmail(user));
								
			return new APIResponse(Status.SUCCESS, prop);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="getAll", httpMethod = HttpMethod.GET)
	public APIResponse getAll(@Named("client") String client) {
		
		try {						
			Client.ensureValidClient(client);
			List<VenueProp> props = Venue.getAll(client);
				
			return new APIResponse(Status.SUCCESS, props);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="rename", httpMethod = HttpMethod.GET)
	public APIResponse update(@Named("client") String client, 
			@Named("venue_id") long venueId, 
			@Nullable @Named("new_short_name") String newDisplayName, 
			@Nullable @Named("new_address") String newAddress, 
			@Nullable @Named("group_id") Long newGroupId, 
			User user) {
		
		try {			
			VenueProp prop = Venue.update(client, venueId, newDisplayName, 
					newAddress, newGroupId, Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, prop);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="delete", httpMethod = HttpMethod.GET)
	public APIResponse delete(@Named("client") String client, 
			@Named("venue_id") long venueId, User user) {
		
		try {
			Client.ensureValidClient(client);
			Venue.delete(client, venueId, Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, "Venue [" + venueId + "] deleted");			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}		
}
